package service;

import domain.*;

import javax.swing.*;
import java.util.Arrays;

/**
 * @作者:侯国威
 * @时间: 2020/4/10/11:43
 * @邮箱:1045077189@qq.com
 */
public class NameListService {

    private Employee[] employee; //保存员工数据

    //给employee即数组元素初始化


    public NameListService() {
        //根据data大小构建数组
        employee = new Employee[Data.EMPLOPYEES.length];
        for (int i = 0; i < employee.length; i++) {
            //获取Employee的四个基本信息
            int type = Integer.parseInt(Data.EMPLOPYEES[i][0]);

            int id = Integer.parseInt(Data.EMPLOPYEES[i][1]);
            String name = Data.EMPLOPYEES[i][2];
            int age = Integer.parseInt(Data.EMPLOPYEES[i][3]);
            double sarlay = Double.parseDouble(Data.EMPLOPYEES[i][4]);
            double bonus;
            int stock;
            switch (type) {
                case Data.Employees:
                    employee[i] = new Employee(id, name, age, sarlay);
                    break;

                case Data.programmers:
                    Equipment equipment = createEquipment(i);
                    employee[i] = new Programmer(id, name, age, sarlay,equipment);
                    break;
                case Data.designers:
                    Equipment equipment2 = createEquipment(i);
                    bonus = Double.parseDouble(Data.EMPLOPYEES[i][4]);
                    employee[i] = new Designer(id, name, age, sarlay, equipment2, bonus);
                case Data.architects:
                    Equipment equipment1 = createEquipment(i);
                    bonus = Double.parseDouble(Data.EMPLOPYEES[i][4]);
                    employee[i] = new Designer(id, name, age, sarlay, equipment1, bonus);

            }
        }
        // 创建对象
        //将对象存入数组中这个

    }

    private Equipment createEquipment(int index) {
        int type = Integer.parseInt(Data.shebei[index][0]);
        switch (type) {
            case Data.pcs:
                return new Pc(Data.shebei[index][1], Data.shebei[index][2]);

            case Data.notebooks:
                return new Notebook(Data.shebei[index][1], Data.shebei[index][2]);
            case Data.printers:
                return new Printer(Data.shebei[index][1], Data.shebei[index][2]);

        }
        return null;
    }


    public NameListService(Employee[] employee) {

        this.employee = employee;
    }

    public Employee[] getEmployee() {
        return employee;
    }

    public void setEmployee(Employee[] employee) {
        this.employee = employee;
    }

    /**
     * @作者:侯国威
     * @时间：2020/4/10_12:38
     * @描述:获取所有员工
     */
    public Employee[] getAllEmployee() {
        return employee;
    }

    public Employee getEmployee(int id) {
        /**
         *@作者:侯国威
         *@时间：2020/4/10_12:40
         *@描述:获取指定员
         */
        for (int i = 0; i < employee.length; i++) {
            if (employee[i].getId() == id) {
                return employee[i];
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return "NameListService{" +
                "employee=" + Arrays.toString(employee) +
                '}';
    }
}
